home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / ncurses-5.3.lha / ncurses-5.3 / c++ / cursespad.cc < prev    next >
C/C++ Source or Header  |  2002-10-24  |  7KB  |  277 lines

  1. // * this is for making emacs happy: -*-Mode: C++;-*-
  2. /****************************************************************************
  3.  * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc.              *
  4.  *                                                                          *
  5.  * Permission is hereby granted, free of charge, to any person obtaining a  *
  6.  * copy of this software and associated documentation files (the            *
  7.  * "Software"), to deal in the Software without restriction, including      *
  8.  * without limitation the rights to use, copy, modify, merge, publish,      *
  9.  * distribute, distribute with modifications, sublicense, and/or sell       *
  10.  * copies of the Software, and to permit persons to whom the Software is    *
  11.  * furnished to do so, subject to the following conditions:                 *
  12.  *                                                                          *
  13.  * The above copyright notice and this permission notice shall be included  *
  14.  * in all copies or substantial portions of the Software.                   *
  15.  *                                                                          *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
  17.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
  18.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
  19.  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
  20.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
  21.  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
  22.  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
  23.  *                                                                          *
  24.  * Except as contained in this notice, the name(s) of the above copyright   *
  25.  * holders shall not be used in advertising or otherwise to promote the     *
  26.  * sale, use or other dealings in this Software without prior written       *
  27.  * authorization.                                                           *
  28.  ****************************************************************************/
  29.  
  30. /****************************************************************************
  31.  *   Author: Juergen Pfeifer, 1999                                          *
  32.  *   Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en             *
  33.  ****************************************************************************/
  34.  
  35. #include "internal.h"
  36. #include "etip.h"
  37. #include "cursesw.h"
  38.  
  39. MODULE_ID("$Id: cursespad.cc,v 1.6 2002/09/22 19:32:55 tom Exp $")
  40.  
  41. NCursesPad::NCursesPad(int lines, int cols)
  42.   : NCursesWindow(),
  43.     viewWin((NCursesWindow*)0),
  44.     viewSub((NCursesWindow*)0),
  45.     h_gridsize(0), v_gridsize(0),
  46.     min_row(0), min_col(0)
  47. {
  48.   w = ::newpad(lines,cols);
  49.   if ((WINDOW*)0==w) {
  50.     count--;
  51.     err_handler("Cannot construct window");
  52.   }
  53.   alloced = TRUE;
  54. }
  55.  
  56.  
  57. int NCursesPad::driver (int key)
  58. {
  59.   // Default implementation
  60.   switch(key) {
  61.   case KEY_UP:
  62.     // =======
  63.     return REQ_PAD_UP;
  64.   case KEY_DOWN:
  65.     // =========
  66.     return REQ_PAD_DOWN;
  67.   case KEY_LEFT:
  68.     // =========
  69.     return REQ_PAD_LEFT;
  70.   case KEY_RIGHT:
  71.     // ==========
  72.     return REQ_PAD_RIGHT;
  73.   case KEY_EXIT:
  74.     // =========
  75.   case CTRL('X'):
  76.     // ==========
  77.     return REQ_PAD_EXIT;
  78.  
  79.   default: return(key);
  80.   }
  81. }
  82.  
  83.  
  84. void NCursesPad::operator()(void)
  85. {
  86.   NCursesWindow* W = Win();
  87.  
  88.   if ((NCursesWindow*)0 != W) {
  89.     int Width  = W->width();
  90.     int Height = W->height();
  91.  
  92.     int req = REQ_PAD_REFRESH;
  93.  
  94.     W->keypad(TRUE);
  95.     W->meta(TRUE);
  96.     refresh();
  97.  
  98.     do {
  99.       bool changed = FALSE;
  100.  
  101.       switch (req) {
  102.       case REQ_PAD_REFRESH:
  103.     // ================
  104.     changed = TRUE;
  105.     break;
  106.       case REQ_PAD_LEFT:
  107.     // =============
  108.     if (min_col > 0) {
  109.       changed = TRUE;
  110.       if (min_col < h_gridsize)
  111.         min_col = 0;
  112.       else
  113.         min_col -= h_gridsize;
  114.     }
  115.     else
  116.       OnNavigationError(req);
  117.     break;
  118.       case REQ_PAD_RIGHT:
  119.     // ==============
  120.     if (min_col < (width() - Width - 1)) {
  121.       changed = TRUE;
  122.       if (min_col > (width() - Width - h_gridsize - 1))
  123.         min_col = width() - Width - 1;
  124.       else
  125.         min_col += h_gridsize;
  126.     }
  127.     else
  128.       OnNavigationError(req);
  129.     break;
  130.       case REQ_PAD_UP:
  131.     // ===========
  132.     if (min_row > 0) {
  133.       changed = TRUE;
  134.       if (min_row < v_gridsize)
  135.         min_row = 0;
  136.       else
  137.         min_row -= v_gridsize;
  138.     }
  139.     else
  140.       OnNavigationError(req);
  141.     break;
  142.       case REQ_PAD_DOWN:
  143.     // =============
  144.     if (min_row < (height() - Height - 1)) {
  145.       changed = TRUE;
  146.       if (min_row > (height() - Height - v_gridsize - 1))
  147.         min_row = height() - Height - 1;
  148.       else
  149.         min_row += v_gridsize;
  150.     }
  151.     else
  152.       OnNavigationError(req);
  153.     break;
  154.  
  155.       default:
  156.     OnUnknownOperation(req);
  157.       }
  158.  
  159.       if (changed) {
  160.     noutrefresh();
  161.     W->syncup();
  162.     OnOperation(req);
  163.     viewWin->refresh();
  164.       }
  165.     } while( (req=driver(W->getch())) != REQ_PAD_EXIT );
  166.   }
  167. }
  168.  
  169.  
  170. int NCursesPad::refresh()
  171. {
  172.   int res = noutrefresh();
  173.   if (res==OK && ((NCursesWindow*)0 != viewWin)) {
  174.     res = (viewWin->refresh());
  175.   }
  176.   return(res);
  177. }
  178.  
  179. int NCursesPad::noutrefresh()
  180. {
  181.   int res = OK;
  182.   NCursesWindow* W = Win();
  183.   if ((NCursesWindow*)0 != W) {
  184.     res = copywin(*W,min_row,min_col,
  185.           0,0,W->maxy(),W->maxx(),
  186.           FALSE);
  187.     if (res==OK) {
  188.       W->syncup();
  189.       res = viewWin->noutrefresh();
  190.     }
  191.   }
  192.   return (res);
  193. }
  194.  
  195. void NCursesPad::setWindow(NCursesWindow& view,
  196.                int v_grid NCURSES_PARAM_INIT(1),
  197.                int h_grid NCURSES_PARAM_INIT(1))
  198. {
  199.   viewWin = &view;
  200.   min_row = min_col = 0;
  201.   if (h_grid <=0 || v_grid <= 0)
  202.     err_handler("Illegal Gridsize");
  203.   else {
  204.     h_gridsize = h_grid;
  205.     v_gridsize = v_grid;
  206.   }
  207. }
  208.  
  209. void NCursesPad::setSubWindow(NCursesWindow& sub)
  210. {
  211.   if ((NCursesWindow*)0 == viewWin)
  212.     err_handler("Pad has no viewport");
  213.   if (!viewWin->isDescendant(sub))
  214.     THROW(new NCursesException("NCursesFramePad", E_SYSTEM_ERROR));
  215.   viewSub = ⊂
  216. }
  217.  
  218. void NCursesFramedPad::OnOperation(int pad_req)
  219. {
  220.   NCursesWindow* W = Win();
  221.   NCursesWindow* Win = getWindow();
  222.  
  223.   if (((NCursesWindow*)0 != W) && ((NCursesWindow*)0 != Win)) {
  224.     int Width  = W->width();
  225.     int Height = W->height();
  226.     int i, row, col, h_len, v_len;
  227.  
  228.     h_len = (Width*Width + width() - 1)/width();
  229.     if (h_len==0)
  230.       h_len = 1;
  231.     if (h_len > Width)
  232.       h_len = Width;
  233.  
  234.     v_len = (Height*Height + height() - 1)/height();
  235.     if (v_len==0)
  236.       v_len = 1;
  237.     if (v_len > Height)
  238.       v_len = Height;
  239.  
  240.     col  = (min_col * Width + width() - 1)  / width();
  241.     if (col + h_len > Width)
  242.       col = Width - h_len;
  243.  
  244.     row  = (min_row * Height + height() - 1) / height();
  245.     if (row + v_len > Height)
  246.       row = Height - v_len;
  247.  
  248.     Win->vline(1,Width+1,Height);
  249.     Win->attron(A_REVERSE);
  250.     if (v_len>=2) {
  251.       Win->addch(row+1,Width+1,ACS_UARROW);
  252.       for(i=2;i<v_len;i++)
  253.     Win->addch(row+i,Width+1,' ');
  254.       Win->addch(row+v_len,Width+1,ACS_DARROW);
  255.     }
  256.     else {
  257.       for(i=1;i<=v_len;i++)
  258.     Win->addch(row+i,Width+1,' ');
  259.     }
  260.     Win->attroff(A_REVERSE);
  261.  
  262.     Win->hline(Height+1,1,Width);
  263.     Win->attron(A_REVERSE);
  264.     if (h_len >= 2) {
  265.       Win->addch(Height+1,col+1,ACS_LARROW);
  266.       for(i=2;i<h_len;i++)
  267.     Win->addch(Height+1,col+i,' ');
  268.       Win->addch(Height+1,col+h_len,ACS_RARROW);
  269.     }
  270.     else {
  271.       for(i=1;i<=h_len;i++)
  272.     Win->addch(Height+1,col+i,' ');
  273.     }
  274.     Win->attroff(A_REVERSE);
  275.   }
  276. }
  277.